<template>
  <div style="padding: 40px">
    <div
      class="mb20"
      style="font-weight: 700; font-style: normal; font-size: 16px"
    >
      检验及检查报告
    </div>
    <el-table
      v-loading="loading"
      :data="documentRetrievealList"
      @selection-change="handleSelectionChange"
      border
    >
      <el-table-column
        label="检验及检查名称"
        align="center"
        prop="documentTitle"
      />
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateLastTime"
      >
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['workstation:documentSearch:report']"
          >查看
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改检验检查报告检索对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <div style="display: flex;justify-content: center;">
        <img :src="src" alt=""/>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDocumentRetrieveal,
  lookreport,
  getDocumentRetrieveal,
  delDocumentRetrieveal,
  addDocumentRetrieveal,
  updateDocumentRetrieveal
} from '@/api/workstation/documentRetrieveal'

export default {
  name: 'DocumentRetrieveal',
  props: ['medicalRecodeNo'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检验检查报告检索表格数据
      documentRetrievealList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        medicalRecordNo: this.medicalRecodeNo,
        reportType: undefined,
        documentId: undefined,
        documentPath: undefined,
        documentTitle: undefined,
        documentType: undefined,
        updateLastUser: undefined,
        updateLastTime: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [{ required: true, message: '检索ID不能为空', trigger: 'blur' }],
        medicalRecordNo: [
          { required: true, message: '病历号不能为空', trigger: 'blur' }
        ],
        reportType: [
          { required: true, message: '报告类型不能为空', trigger: 'change' }
        ],
        documentId: [
          { required: true, message: '文档ID不能为空', trigger: 'blur' }
        ],
        documentPath: [
          { required: true, message: '文档路径不能为空', trigger: 'blur' }
        ],
        documentTitle: [
          {
            required: true,
            message: '检验及检查名称不能为空',
            trigger: 'blur'
          }
        ],
        documentType: [
          { required: true, message: '文档类型不能为空', trigger: 'change' }
        ],
        updateLastUser: [
          { required: true, message: '最后更新人不能为空', trigger: 'blur' }
        ],
        updateLastTime: [
          { required: true, message: '更新时间不能为空', trigger: 'blur' }
        ]
      },
      src: ''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询检验检查报告检索列表 */
    getList() {
      this.loading = true
      listDocumentRetrieveal(this.queryParams).then((response) => {
        this.documentRetrievealList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        medicalRecordNo: undefined,
        reportType: undefined,
        documentId: undefined,
        documentPath: undefined,
        documentTitle: undefined,
        documentType: undefined,
        updateLastUser: undefined,
        updateLastTime: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加检验检查报告检索'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true
      this.reset()
      const id = row.id || this.ids
      lookreport(row.documentId).then((res) => {
        this.loading = false
        this.src = res.data.documentContent
        this.open = true
        this.title = '查看检验检查报告'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true
          if (this.form.id != null) {
            updateDocumentRetrieveal(this.form)
              .then((response) => {
                this.$modal.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
              .finally(() => {
                this.buttonLoading = false
              })
          } else {
            addDocumentRetrieveal(this.form)
              .then((response) => {
                this.$modal.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
              .finally(() => {
                this.buttonLoading = false
              })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否确认删除检验检查报告检索编号为"' + ids + '"的数据项？')
        .then(() => {
          this.loading = true
          return delDocumentRetrieveal(ids)
        })
        .then(() => {
          this.loading = false
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {
        })
        .finally(() => {
          this.loading = false
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'workstation/documentRetrieveal/export',
        {
          ...this.queryParams
        },
        `documentRetrieveal_${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>
